var app = getApp();
const apiHost = require('../../../../../common/api_host.js');
var utils = require('../../../../../utils/utils.js');
var httpJson=require('../../../../../utils/http_json.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme:"萌物",
    
  
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    circular: false,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
    pageNum:0

   
  },
  // 把主题从接口中抽取出来放在theme中
  same_people_add_talk:function(e){
    wx.navigateTo({
      url: '../../../room/add/add?myid=2'
    })
  },
  getList:function(pageNum){
    var  url=`${apiHost.config.portalApiHost}/portal/discuss/content`;
    let data={pageNum,pageSize:10}
    httpJson.get(url,data,res=>{
      if(res.data.errorCode==0){
        setTimeout(()=>{
          wx.hideLoading()
          wx.stopPullDownRefresh()
          // utils.successToast('刷新成功',1000)
        },1000)
       
        this.setData({
          scroll_width:res.data.body.rows.length*650+'rpx'
        })
           /**start1套路代码 */
           if(pageNum==0){
            this.data.list=res.data.body.rows
          }
          /**判断是否还有数据 */
          if(res.data.body.totalPage==this.data.pageNum+1){
            this.setData({
              pasthasMore:false
            })
          }
          /**此时进行分页处理 */
          if(data.pageNum>0){
            this.data.list= this.data.list.concat(res.data.body.rows);
          }
      /**end1套路代码 */
        let list=res.data.body.rows
        let imgList=[]
        list.forEach((ele,idx) => {
          imgList.push(ele.img)
         
          
        });
        this.setData({
          list:this.data.list,
          imgList,
          totalCount:res.data.body.totalCount
        })
      }
    })
  },
  getMore:function(e){
    if(e.detail.current>=this.data.list.length-1){
      if(this.data.list.length<this.data.totalCount){
            this.setData({
            hsaMore:true
            })
          
          let pageNum=++this.data.pageNum
            this.getList(pageNum)
        }
    }
  
    
    console.log('!@@!@',e)

  },

  addLikeContent:function(e){
   
    let contentId=app.E(e,"myid");
    let userLike=app.E(e,'islike');
    let index=app.E(e,'idx');
    console.log(contentId,userLike,index)
    var  url;
    if(userLike){
      url =`${apiHost.config.portalApiHost}/portal/content/removeLike`;
      let data={contentId}
      httpJson.post(url,data,res=>{
        if(res.data.errorCode==0){
          utils.successToast('取消惊叹成功',500)
          var params={};
          var str=`list[${index}].userLike`;

          params[str]=false;
         
        this.setData(params)
        this.data.list[index].likeNum--
        this.setData({
          list:this.data.list
        })

        }
      })
    }
    if(!userLike){
      url =`${apiHost.config.portalApiHost}/portal/content/addLike`;
      let data={contentId}
      httpJson.post(url,data,res=>{
        if(res.data.errorCode==0){
          utils.successToast('惊叹成功',500)
          var params={};
          var str=`list[${index}].userLike`;
          params[str]=true;
        this.setData(params)
        this.data.list[index].likeNum++
        this.setData({
          list:this.data.list
        })
        }
      })
    }
  },
  previewImage : function (e) {
    let idx=e.currentTarget.dataset['idx']
   
    var { imgList} = this.data;
   
   
  
    wx.previewImage({
      current:imgList[idx],
      urls:imgList
    });
  },
  formSubmit: function(e) {


    var formId = e.detail.formId;
  
    let path = `${apiHost.config.portalApiHost}/portal/userformId/add`;
    httpJson.post(path, { 'formId': formId}, function (data) {
      let res = data.data;
     
    })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
   
    
       // 动态设置填充高度
       const res = wx.getSystemInfoSync()
       let content_height=(res.windowHeight * (750 / res.windowWidth))-270 ;
       let wrap_content_height=(res.windowHeight * (750 / res.windowWidth));
       this.setData({
         
          content_height: content_height + "rpx",
          wrap_content_height:wrap_content_height+"rpx"
       })
     

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList(0)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({

      title: '正在刷新',
    })
    
 this.getList(0)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    app.share()
    if (res.from === 'button') {
      var content=res.target.dataset.content
     
      if(content.length>50){
        content=content.substr(0,50)+'...'
      }
      var title=content
    }
    if(res.from === 'menu'){
      var imageUrl="/images/index/reply/reply.png";
      var title='我正在惊人院，邀请你来……'
    }
    return {
      title,
      imageUrl
      
    }
  }
})